package me.linkcn.ydtxl.server.resources;

import me.linkcn.ydtxl.server.objects.Group;
import me.linkcn.ydtxl.server.objects.ObjectsFacade;
import org.codehaus.jackson.map.ObjectMapper;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: cmcc
 * Date: 11-8-15
 * Time: 上午12:37
 */
public class GroupsResource extends BaseResource {
    String accountName;

    @Override
    protected void doInit() throws ResourceException {
        this.accountName = (String) getRequest().getAttributes().get("accountName");
    }

    @Get("json")
    public Representation getGroupList() {
        allowOtherHost();
        ObjectsFacade dataFacade = getObjectsFacade();
        List<Group> groupList = dataFacade.getGroupListByAccountName(this.accountName);
        String result = null;
        if (groupList.size() > 0) {
            try {
                ObjectMapper mapper = new ObjectMapper();
                result = mapper.writeValueAsString(groupList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new StringRepresentation(result);
    }
}
